package com.navinfo.data.esper.cep.linkid;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.*;

public class LinkidMainVertexByList {
    private static final Logger log = LoggerFactory.getLogger(LinkidMainVertexByList.class);

    /**
     * 获取包含同样次序linkid序列的tid列表
     * @param map tid及其linkid列表
     * @param roadLinkids 待查的次序linkid列表
     * @return 满足条件的tid列表
     */
    public static List<Integer> getLinkidList(List<Integer> tList,List<List<Integer>> linkidList,List<Integer> roadLinkids) {
        List<Integer> tidList=new ArrayList<Integer>();

        /**
         * 定点及其经过tid列表
         */
        Map<Integer, List<VisitBean>> roadLinkidMap = new HashMap<Integer, List<VisitBean>>();

        if (roadLinkids.size()<1) {
            return new ArrayList<Integer>();
        }

        for(int z=0;z<roadLinkids.size();z++){
           roadLinkidMap.put(roadLinkids.get(z),new ArrayList<VisitBean>());
        }
        int count = 0;
        int tid = 0;
//        for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
        for(int z=1;z<tList.size();z++){
//            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            tid = tList.get(z);
            for(Integer mapkey : linkidList.get(z)){
                if (roadLinkidMap.containsKey(mapkey)) {
                    roadLinkidMap.get(mapkey).add(new VisitBean(count++,tid));
                }
            }
        }
        List<VisitBean> vbl2= null;
        int vbflag = 0;
        if (roadLinkids.size()>1){
            List<VisitBean> vbl= roadLinkidMap.get(roadLinkids.get(0));

            for(VisitBean vb : vbl){
                vbflag = 1;
                tid = vb.getTid();
                count = vb.getTime();
                for(int z=1;z<roadLinkids.size();z++){
                    vbl2 = roadLinkidMap.get(roadLinkids.get(z));
                    if (vbl2.size()==0) {
                        break;
                    } else {
                        for(VisitBean vb2 : vbl2){
                            if ((vb2.getTid()==tid)&&(vb2.getTime()>count)) {
                                vbflag++;
                                break;
                            }
                        }
                    }
                }
                if (vbflag==roadLinkids.size()) {
                    tidList.add(tid);
                }
            }
        } else if (roadLinkids.size()==1){
            List<VisitBean> vbl= roadLinkidMap.get(roadLinkids.get(0));
            for(VisitBean vb : vbl){
                tidList.add(vb.getTid());
            }

        }
        Collections.sort(tidList);
        return tidList;

    }

    public static void main(String[] args) throws IOException, InterruptedException {

        log.info("making test data begin......");
        //创建测试数据集
//        Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
        List<Integer> tidList=new ArrayList<Integer>();
        List<List<Integer>> linkidList=new ArrayList<List<Integer>>();
        for (int i = 700001; i < 800110; i++) {
            List<Integer> li = new ArrayList<Integer>(3000);
            for (int j = 1; j < 2400; j++) {
                li.add(j+901099-i);
            }
            tidList.add(i);
            linkidList.add(li);
//            map.put(i,li);
        }

        List<Integer> roadLinkids=new ArrayList<Integer>(100);
        for (int j = 1; j < 10; j++) {
            roadLinkids.add(200000-1+j);
        }

        log.info("making test data end......");

        log.info("getting tid list begin ......");
        List<Integer> unique= getLinkidList(tidList,linkidList,roadLinkids);
        log.info("getting tid list end ......");

        log.info("printing tid list begin ......");
        Iterator<Integer> it2 = unique.iterator();
        while (it2.hasNext()) {
            log.info(it2.next() + " ");
        }
        log.info("printing tid list end ......");
    }
}
